package butines.kraz;

import java.awt.Color;
import java.awt.Graphics2D;

import butines.core.GameSystem;
import butines.core.Sprite;
import butines.core.SpriteSheet;


public class Selector {

	private boolean pressed;
	private boolean dragged;
	
	private int pressedX;
	private int pressedY;
	
	private int screenX;
	private int screenY;
	
	private int lastX;
	private int lastY;

	private int height;
	private int width;

	private Color colorInner  = new Color(.0f, .95f, .0f, .20f);
	private Color colorBorder = new Color(.0f, .90f, .0f, .50f);
	
	private Sprite cursorArrow;
	private Sprite cursorDrag;
	private Sprite cursorOver;
	
	public Selector() {
		SpriteSheet siCursorArrow = new SpriteSheet("datas/ui/cursor_arrow.png", 128, 128);
		siCursorArrow.addAnimation(true, .5f, 0, 1, 2, 3, 4);
		
		SpriteSheet siCursorDrag = new SpriteSheet("datas/ui/cursor_drag.png", 128, 128);
		siCursorDrag.addAnimation(true, .5f, 0, 1, 2, 3);
		
		SpriteSheet siCursorOver = new SpriteSheet("datas/ui/cursor_over.png", 128, 128);
		siCursorOver.addAnimation(true, 1.4f, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13);
		
		cursorArrow = new Sprite(siCursorArrow);
		cursorDrag  = new Sprite(siCursorDrag);
		cursorOver  = new Sprite(siCursorOver);
		
		GameSystem.getContext().setCursor(cursorArrow);
	}
	
	public boolean isPressed() {
		return pressed;
	}

	public boolean isDragged() {
		return dragged;
	}

	public void onPressed(int x, int y) {
		pressedX = x;
		pressedY = y;
		width = 0;
		height = 0;
		pressed = true;
		dragged = false;
	}
	
	public void onReleased() {
		pressed = false;
		dragged = false;
		GameSystem.getContext().setCursor(cursorArrow);
	}
	
	public void onMove(int x, int y) {
		lastX = x;
		lastY = y;
		if (pressed) {
			updateDrag(x, y);
		}
	}
	
	public void draw(Graphics2D g) {
		if (dragged) {
			g.setColor(colorInner);
			g.fillRect(screenX, screenY, width, height);
			g.setColor(colorBorder);
			g.drawRect(screenX, screenY, width, height);
		}
	}

	
	private void updateDrag(int mouseX, int mouseY) {
		final int maxWidth  = GameSystem.getContext().getScreenWidth();
		final int maxHeight = GameSystem.getContext().getScreenHeight();
		
		mouseX = Math.max(1, Math.min(mouseX, maxWidth - 1));
		mouseY = Math.max(1, Math.min(mouseY, maxHeight - 1));
		
		// Recupera a extremidade esquerda (x) e acima (y)	
		screenX = Math.min(pressedX, mouseX);
		screenY = Math.min(pressedY, mouseY);
		
		// Atualiza o tamanho da area sinalizada
		width = Math.abs(pressedX - mouseX);
		height = Math.abs(pressedY - mouseY);
		
		// Se ocorre evento de arrastar muda o cursor e sinaliza o evento
		if (!dragged && width > 1 && height > 1) {
			dragged = true;
			GameSystem.getContext().setCursor(cursorDrag);
		}
	}

	public int getPressedX() {
		return pressedX;
	}

	public int getPressedY() {
		return pressedY;
	}

	public int getScreenX() {
		return screenX;
	}

	public int getScreenY() {
		return screenY;
	}

	public int getHeight() {
		return height;
	}

	public int getWidth() {
		return width;
	}
	
	public boolean isMouseOver(float x, float y, float radius) {
		return x >= lastX - radius
			&& x <= lastX + radius
			&& y >= lastY - radius
			&& y <= lastY + radius;
	}
	
	public boolean isSelectionOver(float x, float y, float radius) {
		return dragged
			&& x >= screenX - radius
			&& x <= screenX + width  + radius
			&& y >= screenY - radius
			&& y <= screenY + height + radius;
	}

}
